package com.woniuxy.lambda;

import jdk.internal.org.objectweb.asm.Opcodes;

import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/3/31:19:59
 * <P> Description:
 */

public class LambdaDemo {
    public static void main(String[] args) {
//        int cal = cal((left, right) -> left + right);
//
//        cal(new IntBinaryOperator() {
//            @Override
//            public int applyAsInt(int left, int right) {
//                return left + right;
//            }
//        });
        //  System.out.println(cal);
//        printNum(new IntPredicate() {
//            @Override
//            public boolean test(int value) {
//
//                return value % 2 == 0;
//            }
//        });
//        printNum(value -> value % 2 == 0);
        Integer integer = typeConver(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {

                return Integer.parseInt(s);
            }
        });
        typeConver((String s) -> {
            return Integer.parseInt(s);
        });
        System.out.println(integer);
        String str = typeConver(new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s + "san";
            }
        });
        System.out.println(str);
        typeConver(s -> s + "san");

        foreach(new IntConsumer() {
            @Override
            public void accept(int value) {
                System.out.println(value);
            }
        });
        foreach(value -> System.out.println(value)
        );
    }


    public static int cal(IntBinaryOperator operator) {
        int a = 10;
        int b = 10;
        return operator.applyAsInt(a, b);
    }

    public static void printNum(IntPredicate predicate) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            if (predicate.test(i)) {
                System.out.println(i);
            }
        }
    }

    public static <R> R typeConver(Function<String, R> function) {
        String str = "123";
        R result = function.apply(str);
        return result;

    }

    public static void foreach(IntConsumer consumer) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            consumer.accept(i);
        }
    }

}
